package com.wonders;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.io.IOException;

@WebServlet("/UploadServlet")
@MultipartConfig() // 表明当前servlet可以处理文件上传请求
public class UploadServlet extends HttpServlet {

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Part upfile = request.getPart("upfile");
        String contextPath = request.getServletContext().getRealPath("/");
        String fileName = upfile.getHeader("content-disposition").split("filename=")[1].replaceAll("\"", "");
        upfile.write(contextPath + fileName);
    }

}
